package com.frank.wechat.handler;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Frank Zou
 * @version v0.0.1
 * @date 2024/2/21
 * @description
 */
@Component
public class WeChatMessageFactory implements InitializingBean {
    @Resource
    private List<WeChatMessageHandler> weChatMessageHandlerList;

    private final Map<WeChatMessageTypeEnum, WeChatMessageHandler> handlerMap = new HashMap<>();

    public WeChatMessageHandler getHandler(String messageType) {
        WeChatMessageTypeEnum weChatMessageTypeEnum = WeChatMessageTypeEnum.getMessageTypeByType(messageType);
        return handlerMap.get(weChatMessageTypeEnum);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        for (WeChatMessageHandler weChatMessageHandler : weChatMessageHandlerList) {
            handlerMap.put(weChatMessageHandler.getMessageType(), weChatMessageHandler);
        }
    }
}
